<?php

namespace app\controllers;

use yii;
use yii\web\Controller;
use app\lib\BaseFunction;

class UploadController extends ApiBaseController
{
    public $enableCsrfValidation = false;

    public function actionUploadImg()
    {
        if (isset($_FILES['file'])) {
            $file = $_FILES['file'];
            $re = BaseFunction::check_img($file['type']);
            if (!$re) {
                return \json_encode(['code' => 0, 'msg' => '不是图片']);
            }
            $path = BaseFunction::upload($file);
            if ($path) {
                return \json_encode(['code' => 1, 'msg' => $path]);
            } else {
                return \json_encode(['code' => 0, 'msg' => '上传失败,请重试']);
            }
        }
    }

    public function actionUploadFile()
    {
        if (isset($_FILES['file'])) {
            $file = $_FILES['file'];
            $path = BaseFunction::upload($file);
            if ($path) {
                return \json_encode(['code' => 1, 'msg' => $path]);
            } else {
                return \json_encode(['code' => 0, 'msg' => '上传失败,请重试']);
            }
        }
    }

    //上传采购应付账款明细excel
    public function actionUpload_purchase_pay()
    {
        if (isset($_FILES['file'])) {
            $file = $_FILES['file'];
            if (!BaseFunction::check_excel($file['type'])) {
                return json_encode(['code' => 0, 'msg' => "不是excel"]);
            }
            $upload_file = ROOT_PATH . BaseFunction::upload($file);
            $excel = BaseFunction::get_excel($upload_file, explode('.', $upload_file)[1]);
            $sheet = $excel->getSheet(0);
            $highestRow = $sheet->getHighestRow();
            $data = [];
            for ($i = 2; $i < $highestRow; $i++) {
                $date = $excel->getActiveSheet()->getCell('A' . $i)->getValue();
                if (is_numeric($date)) {
                    $date = date('Y-m-d', ($date - 25569) * 24 * 60 * 60);
                }
                if ($date) {
                    $data[$i][] = $date;
                    $data[$i][] = $excel->getActiveSheet()->getCell('B' . $i)->getValue();//负责人
                    $data[$i][] = $excel->getActiveSheet()->getCell('C' . $i)->getValue();//采购编号
                    $data[$i][] = $excel->getActiveSheet()->getCell('D' . $i)->getValue();//供应商
                    $data[$i][] = $excel->getActiveSheet()->getCell('E' . $i)->getValue();//商品
                    $data[$i][] = $excel->getActiveSheet()->getCell('F' . $i)->getValue();//数量
                    $data[$i][] = $excel->getActiveSheet()->getCell('G' . $i)->getValue();//单价
                    $data[$i][] = $excel->getActiveSheet()->getCell('H' . $i)->getValue();//应付金额
                    $data[$i][] = $excel->getActiveSheet()->getCell('I' . $i)->getValue();//已付金额
                    $data[$i][] = $excel->getActiveSheet()->getCell('J' . $i)->getValue();//客户编号
                    $data[$i][] = $excel->getActiveSheet()->getCell('K' . $i)->getValue();//客户名称
                    $data[$i][] = $excel->getActiveSheet()->getCell('L' . $i)->getValue();//备注
                }
            }
            $key = [
                'date', 'purchase', 'purchase_order', 'company', 'product', 'number',
                'price', 'need_pay', 'have_pay', 'customer_num', 'customer_name', 'remark'
            ];
            $table = yii::$app->db->tablePrefix . 'product_pay';
            if (yii::$app->db->createCommand()->batchInsert($table, $key, $data)->execute()) {
                echo json_encode(['code' => '1', 'msg' => '添加成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '添加失败,请重试']);
            }
        }
    }
}
